/**
 * @author Tobias Boehm
 *
 * 12.05.2011
 */
package org.anddev.andengine.extension.tbsengine.message.server.preparematch;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import org.anddev.andengine.extension.tbsengine.message.ICopyable;
import org.anddev.andengine.extension.tbsengine.message.client.preparematch.ClientPrepareMatchRequestMessage;
import org.anddev.andengine.extension.tbsengine.message.server.AbsServerMatchMessage;
import org.anddev.andengine.extension.tbsengine.message.server.DefaultTBSserverMessageFlags;

/**
 * You should use this server message reply to an {@link ClientPrepareMatchRequestMessage}.
 * 
 * @author Tobias Boehm
 *
 */
public class ServerPrepareMatchRequestReplyMessage extends AbsServerMatchMessage
		implements DefaultTBSserverMessageFlags, ICopyable<ServerPrepareMatchRequestReplyMessage> {

	public static enum StartMatchRequestAnswerReason{OK,DIFFERENT_MAP_VERSION,MAP_UNKOWN,NO_PERMISSION,YOU_ARE_ALREADY_A_HOST};
	
	private boolean mAccepted = false;
	private StartMatchRequestAnswerReason mStartMatchRequestAnswerReason = StartMatchRequestAnswerReason.NO_PERMISSION;
	

	
	/* (non-Javadoc)
	 * @see org.anddev.andengine.extension.multiplayer.protocol.adt.message.IMessage#getFlag()
	 */
	@Override
	public short getFlag() {
		
		return FLAG_SERVER_MATCHPREPARATION_REQUEST_REPLY;
	}
	
	@Override
	protected void onReadTransmissonMatchData(DataInputStream pDataInputStream)
			throws IOException {
		
		this.mAccepted = pDataInputStream.readBoolean();
		
		this.mStartMatchRequestAnswerReason = StartMatchRequestAnswerReason.valueOf(pDataInputStream.readUTF());
	}

	@Override
	protected void onWriteTransmissonMatchData(
			DataOutputStream pDataOutputStream) throws IOException {

		pDataOutputStream.writeBoolean(this.mAccepted);
		
		pDataOutputStream.writeUTF(this.mStartMatchRequestAnswerReason.name());
	}

	/**
	 * @return the mAccepted
	 */
	public boolean isAccepted() {
		return mAccepted;
	}

	/**
	 * @param mAccepted the mAccepted to set
	 */
	public void setAccepted(boolean mAccepted) {
		this.mAccepted = mAccepted;
	}

	/**
	 * @return the mStartMatchRequestAnswerReason
	 */
	public StartMatchRequestAnswerReason getStartMatchRequestAnswerReason() {
		return mStartMatchRequestAnswerReason;
	}

	/**
	 * @param mStartMatchRequestAnswerReason the mStartMatchRequestAnswerReason to set
	 */
	public void setStartMatchRequestAnswerReason(
			StartMatchRequestAnswerReason mStartMatchRequestAnswerReason) {
		this.mStartMatchRequestAnswerReason = mStartMatchRequestAnswerReason;
	}

	@Override
	public ServerPrepareMatchRequestReplyMessage copy() {
		
		final ServerPrepareMatchRequestReplyMessage copy = new ServerPrepareMatchRequestReplyMessage();
		
		copy.mAccepted = this.mAccepted;
		copy.mMatchID = this.mMatchID;
		copy.mStartMatchRequestAnswerReason = this.mStartMatchRequestAnswerReason;
		
		return copy;
	}
}
